#!/bin/bash
trap 'echo Abort ; exit 1' ERR
err() { echo "Error. $@ not set."; exit 1; }
. .varlist
[ -z "$TOOLCHAIN_DIR" ] && err TOOLCHAIN_DIR
[ -z "$GCC" ] && err GCC
[ -z "$BUILD_DIR" ] && err BUILD_DIR
[ -z "$SOURCE_DIR" ] && err SOURCE_DIR
[ -z "$HOST" ] && err HOST
[ -z "$TARGET" ] && err TARGET
[ -z "$STATIC_LIB_DIR" ] && err STATIC_LIB_DIR

echo Build gcc2.
[ -f $TOOLCHAIN_DIR/.${GCC}_2 ] && exit 0
export PATH=$PATH:$TOOLCHAIN_DIR/usr/bin/
rm $BUILD_DIR/${GCC}_2 -rf
mkdir $BUILD_DIR/${GCC}_2
cd $BUILD_DIR/${GCC}_2

$SOURCE_DIR/$GCC/configure --prefix=/usr --build=$HOST --host=$HOST \
	--target=$TARGET --with-sysroot=$TOOLCHAIN_DIR \
	--with-gmp=$STATIC_LIB_DIR --with-mpfr=$STATIC_LIB_DIR \
	--disable-multilib --disable-nls --enable-shared \
	--enable-__cxa_atexit --enable-c99 --enable-long-long \
	--enable-threads=posix --enable-languages=c,c++ \
	--with-float=soft --with-cpu=arm926ej-s
	
set -x
make AS_FOR_TARGET=${TARGET}-as LD_FOR_TARGET=${TARGET}-ld 
make DESTDIR=$TOOLCHAIN_DIR install > /dev/null

touch $TOOLCHAIN_DIR/.${GCC}_2
